﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlatForm.DataLayer;

namespace PlatForm.Controller
{
    public class ArticleController
    {
        public List<Article> GetList()
        {
            return Article.Objects.ToList();
        }
        
        public List<Article> GetBySuppl(string SUPPL_No)
        {
            string poNo = PickingOrder.Objects.Where(x => x.SUPPL_No == SUPPL_No && x.DELI_Date_WH==DateTime.Now.Date).Select(x => x.PO_No).FirstOrDefault();

            var ord = Order.Objects.Where(x => x.PO_No == poNo).ToList();

            var lstArt = ord.GroupBy(x => x.ART_No).Select(x => x.Key).ToList();

            var art = Article.Objects.Where(y => lstArt.Contains(y.ART_No)).ToList();

            return art;
        }

        public int Save(Article article)
        {
            var isNew = 1;
            var s = Article.Objects.FirstOrDefault(x => x.ART_No == article.ART_No);
            if (s != null)
            {
                Article.Objects.Delete(x => x.ART_No == article.ART_No);
                isNew = 0;
            }
            article.Insert(true);
            return isNew;
        }

        public void Delete(string ART_No)
        {
            Article.Objects.Delete(x => x.ART_No == ART_No);
        }
    }
}
